//****************************************************************
//**** ADCON
//****************************************************************

void OpenADC ( unsigned char , // ADCON 2
unsigned char , // ADCON 1
unsigned char ); // ADCON 0 & Interrupt ON/OFF
unsigned int ReadADC( void );
void SetChanADC ( unsigned char );

//**** ADCON 2
//*************** A/D Result Format Select ***********************************
#define ADC_RIGHT_JUST 0b11111111 // Right justify A/D result
#define ADC_LEFT_JUST 0b01111111 // Left justify A/D result

//************** A/D Acquisition Time Selection *******************************
#define ADC_0_TAD 0b11000111 //A/D Acquisition Time is 0 TAD
#define ADC_2_TAD 0b11001111 //A/D Acquisition Time is 2 TAD
#define ADC_4_TAD 0b11010111 //A/D Acquisition Time is 4 TAD
#define ADC_6_TAD 0b11011111 //A/D Acquisition Time is 6 TAD
#define ADC_8_TAD 0b11100111 //A/D Acquisition Time is 8 TAD
#define ADC_12_TAD 0b11101111 //A/D Acquisition Time is 12 TAD
#define ADC_16_TAD 0b11110111 //A/D Acquisition Time is 16 TAD
#define ADC_20_TAD 0b11111111 //A/D Acquisition Time is 20 TAD

//*************** A/D Conversion Clock Selection *****************************
#define ADC_FOSC_2 0b11111000 //A/D conversion clock source is Fosc/2
#define ADC_FOSC_8 0b11111001 //A/D conversion clock source is Fosc/8
#define ADC_FOSC_32 0b11111010 //A/D conversion clock source is Fosc/32
//#define ADC_FRC_4 0b11111011 //A/D conversion clock source is FRC/4
#define ADC_FOSC_4 0b11111100 //A/D conversion clock source is Fosc/4
#define ADC_FOSC_16 0b11111101 //A/D conversion clock source is Fosc/16
#define ADC_FOSC_64 0b11111110 //A/D conversion clock source is Fosc/64
#define ADC_FOSC_RC 0b11111111 //A/D conversion clock source is Internal RC OSC

//**** ADCON 1
#define ADC_VREFPLUS_VDD ADC_REF_VDD_VDD // VREF+ = AVDD
#define ADC_VREFPLUS_EXT ADC_REF_VDD_VREFPLUS // VREF+ = external
#define ADC_VREFMINUS_VSS ADC_REF_VDD_VSS // VREF- = AVSS
#define ADC_VREFMINUS_EXT ADC_REF_VDD_VREFMINUS // VREF- = external

//************** Positive Voltage Reference Configuration *************************
#define ADC_REF_VDD_VDD 0b11110011 // ADC voltage source VREF+ = AVDD
#define ADC_REF_VDD_VREFPLUS 0b11110111 // ADC voltage source VREF+ = ext. source at VREF+
#define ADC_REF_VDD_FVREF 0b11111011 // ADC voltage source VREF+ = FVREF+

//************** Negetive Voltage Reference Configuration *************************
#define ADC_REF_VDD_VSS 0b11111100 // ADC voltage source VREF- = AVSS
#define ADC_REF_VDD_VREFMINUS 0b11111101 // ADC voltage source VREF- = ext. source at VREF-

//**** ADCON 0
//**************** channel selection ******************************************
#define ADC_CH3 0b11001111 //Select Channel 3
#define ADC_CH4 0b11010011 //Select Channel 4
#define ADC_CH5 0b11010111 //Select Channel 5
#define ADC_CH6 0b11011011 //Select Channel 6
#define ADC_CH7 0b11011111 //Select Channel 7
#define ADC_CH8 0b11100011 //Select Channel 8
#define ADC_CH9 0b11100111 //Select Channel 9
#define ADC_CH10 0b11101011 //Select Channel 10
#define ADC_CH11 0b11101111 //Select Channel 11
#define DAC1 0b11111011 // Digital to Analog convertor
#define FVR1 0b11111111 // Fixed Voltage Regulator

//*************** ADC Enable/Disable *******************************
#define ADC_Enable 0b11111111 //A/D Enable
#define ADC_Disable 0b11111110 //A/D Disable

//*************** ADC Interrupt Enable/Disable *******************************
#define ADC_INT_ON 0b11111111 //A/D Interrupt Enable
#define ADC_INT_OFF 0b01111111 //A/D Interrupt Disable

#define ADC_INT_ENABLE() (PIR1bits.ADIF=0,INTCONbits.PEIE=1,PIE1bits.ADIE=1)
#define ADC_INT_DISABLE() (PIE1bits.ADIE=0)

//****************** ȉ֐ *************************************
#define CloseADC() (ADCON0bits.ADON=0,PIE1bits.ADIE=0)
#define ConvertADC() (ADCON0bits.GO=1)
#define BusyADC() (ADCON0bits.NOT_DONE)

//****************************************************************
void OpenADC ( unsigned char rADCON2,
unsigned char rADCON1,
unsigned char rADCON0){
rADCON0 &= 0b11111101; // GO = 0, ADON =1
if ((rADCON0 & 0b10000000) == 0){
ADC_INT_DISABLE();
}else{
ADC_INT_ENABLE();
}
ADCON2 = rADCON2;
ADCON1 = rADCON1;
ADCON0 = rADCON0;
}

//****************************************************************
unsigned int ReadADC( void ){
return ((ADRESH*256) + ADRESL);
}

//****************************************************************
void SetChanADC ( unsigned char ChCode){
ADCON0 = (ADCON0 | 0b00111100) & ChCode;
}